package com.bfxy.rabbitmq.message;

import com.bfxy.rabbitmq.util.RabbitUtil;
import com.rabbitmq.client.*;

import java.util.Map;

/**
 * Created by deng_wb on 2020/3/9
 */
@SuppressWarnings("all")
public class Consumer  {
    public static void main(String[] args) throws Exception{

        Channel channel = RabbitUtil.consumerChannel();

        // 4. 声明 (创建) 一个队列
        String queueName = "test001";
        channel.queueDeclare(queueName,true,false,false,null);

        // 5. 创建消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);

        // 6. 设置Channel
        channel.basicConsume(queueName,true,queueingConsumer);

        // 7. 获取消息

        while (true){
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());

            Envelope envelope = delivery.getEnvelope();
            // 手动签收( ACK )时使用
            long deliveryTag = envelope.getDeliveryTag();
            System.out.println("消费端: "+msg+" and deliveryTag is: "+deliveryTag);

            Map<String, Object> headers = delivery.getProperties().getHeaders();
            headers.forEach((k,v)->{

                System.out.println(k+":"+v);
            });
        }

    }
}
